<style scoped>
.roleManagement {
  padding: 10px;
}
</style>

<template>
  <div class="manage-wrap">
    <div class="operation-panel">
      <Row>
        <Col span="24">
          <Button
            class="float-r"
            type="primary"
            size="large"
            icon="ios-search"
            @click="doSearchTableList"
          >搜 索</Button>
          <Button
            class="float-r"
            size="large"
            icon="md-add"
            @click="addRole"
            v-perms="'sys:role:save'"
            style="margin-right:10px;"
          >添加</Button>
        </Col>
      </Row>
    </div>
    <!--table-->
    <Row>
      <Col :xs="24" :sm="24" :md="18" :lg="12">
        <Row>
          <Table stripe size="small" :columns="tableColums" :data="tableData"></Table>
        </Row>
        <Row>
          <Page
            :total="total"
            :page-size="currentPageSize"
            :current="currentPage"
            @on-change="changeCurrentPage"
            @on-page-size-change="changePageSize"
            show-total
            show-sizer
            style="float:right;margin-top:10px"
          ></Page>
        </Row>
      </Col>
    </Row>
    <!--新增编辑-->
    <roleForm
      :modalShow="formShow"
      :modalFormTitle="formTitle"
      :parentForm="parentForm"
      @listenModalForm="hideModel"
      @refreshTableList="getTableList"
    ></roleForm>
    <!--是否删除框-->
    <Modal v-model="delModal" width="360">
      <p slot="header" style="color:#f60;text-align:center">
        <Icon type="information-circled"></Icon>
        <span>删除确认</span>
      </p>
      <div style="text-align:center">
        <p>是否继续删除？</p>
      </div>
      <div slot="footer">
        <Button type="error" size="large" long :loading="btnLoading" @click="comfirmDel">删除</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import { getRoleList, delRole } from "@/api/system";
import roleForm from "./roleForm.vue";
export default {
  components: {
    roleForm
  },
  data() {
    return {
      tableColums: [
        {
          type: "index",
          title: "序号",
          align: "center"
        },
        {
          align: "center",
          title: "角色名称",
          key: "roleName"
        },
        // {
        //   align: "center",
        //   title: "所属部门",
        //   key: "deptName"
        // },
        {
          align: "center",
          title: "备注",
          key: "remark"
        },
        {
          title: "操作",
          align: "center",
          render: (h, params) => {
            let actions = [];

            actions.push(
              h(
                "Button",
                {
                  props: {
                    type: "warning",
                    size: "small"
                  },
                  directives: [
                    {
                      name: "perms",
                      value: "sys:role:update"
                    }
                  ],
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.editRole(params.row);
                    }
                  }
                },
                "修改"
              )
            );
            if (params.row.roleId != 1) {
              actions.push(
                h(
                  "Button",
                  {
                    props: {
                      type: "error",
                      size: "small"
                    },
                    directives: [
                      {
                        name: "perms",
                        value: "sys:role:delete"
                      }
                    ],
                    on: {
                      click: () => {
                        this.dodelRole(params.row.roleId);
                      }
                    }
                  },
                  "删除"
                )
              );
            }

            return h("div", actions);
          }
        }
      ],
      tableData: [
        {
          RoleName: "客户属性",
          RoleNo: ""
        }
      ],
      formShow: false,
      formTitle: "添加角色",
      parentForm: {
        roleId: "",
        roleName: "",
        deptId: "",
        menuIdList: [],
        deptIdList: [],
        depCascader: "",
        remark: ""
      },
      resetForm: {
        roleId: "",
        roleName: "",
        deptId: "",
        menuIdList: [],
        deptIdList: [],
        depCascader: "",
        remark: ""
      },
      delModal: false,
      delId: "", //删除的Id
      btnLoading: false,
      searchForm: {
        roleName: "",
        rows: 10,
        page: 1
      },
      total: 0,
      currentPage: 1,
      currentPageSize: 10,
      pageSize: 10
    };
  },
  created() {},
  mounted() {
    this.getTableList();
  },
  methods: {
    async getTableList() {
      const params = {
        size: this.currentPageSize,
        current: this.currentPage
      };
      const result = await getRoleList(params);
      if (result.code == 0) {
        let res = result.data;
        this.total = res.total;
        this.tableData = res.records;
      }
    },
    changeCurrentPage(num) {
      this.currentPage = num;
      this.getTableList();
    },
    changePageSize(pageSize) {
      this.currentPageSize = pageSize;
      this.getTableList();
    },
    addRole() {
      this.parentForm = JSON.parse(JSON.stringify(this.resetForm));
      this.formTitle = "添加角色";
      this.formShow = true;
    },
    editRole(row) {
      this.parentForm = Object.assign(row);
      this.formTitle = "修改角色";
      this.formShow = true;
    },
    dodelRole(Id) {
      this.delId = Id;
      this.delModal = true;
    },
    doSearchTableList() {
      this.getTableList();
    },
    async comfirmDel() {
      this.btnLoading = true;
      try {
        const res = await delRole(this.delId);
        if (res.code == 0) {
          this.$Message.success("删除成功!");
          this.getTableList();
          this.delModal = false;
        } else {
          this.$Message.error(res.msg);
        }
      } catch (err) {
        console.log(err);
        this.$Message.error("服务器异常，稍后再试");
      }
      this.btnLoading = false;
    },
    hideModel() {
      this.formShow = false;
    }
  }
};
</script>
